<?php
/**
 * AutoLoad.class.php
 * 
 * PDK自动加载引导类
 * 
 * @author lsl <470663151@qq.com> 
 * @copyright Copyright (c) 2012 lsl8966 Software Studio
 * @link http://www.cnblogs.com/lsl8966
 * @version $Id: AutoLoad.class.php 1.0 2012-12-18 22:48:01Z lsl8966 $
 * @package core
 * @since 1.0
 */

if (!defined('IN_PDK')){
     exit();
    }

class AutoLoad{
    
    /**
     * pdk核心类引导数组

     * 用于自动加载文件运行时,引导路径
     * 
     * @var array 
     */
     public static $coreClassArray = array(
            'Request'       => 'lib/core/Request.class.php',
            'Model'         => 'lib/core/Model.class.php',
            'db_mysqli'     => 'lib/core/db/db_mysqli.class.php',
            'db_mysql'      => 'lib/core/db/db_mysql.class.php',
            'db_pdo'        => 'lib/core/db/db_pdo.class.php',
            'pdb'           => 'lib/tool/pdb.class.php',
            'Log'           => 'lib/core/Log.class.php',
            'Widget'        => 'lib/core/Widget.class.php',
            'Service'        => 'lib/core/Service.class.php',
            'View'          => 'lib/core/View.class.php',
            'Template'      => 'lib/core/Template.class.php',
            'WidgetTemplate'=> 'lib/core/WidgetTemplate.class.php',
            'Module'        => 'lib/core/Module.class.php',
            'script'        => 'lib/tool/script.class.php',
            'html'          => 'lib/tool/html.class.php',
            'pager'         => 'lib/tool/pager.class.php',
            'cookie'        => 'lib/tool/cookie.class.php',
            'session'       => 'lib/tool/session.class.php',
            'cache'         => 'lib/tool/cache/cache.class.php',
            'cache_db'      => 'lib/tool/cache/cache_db.class.php',
            'cache_php'     => 'lib/tool/cache/cache_php.class.php',
            'cache_file'    => 'lib/tool/cache/cache_file.class.php',
            'configure'     => 'lib/tool/configure.class.php',
            'file'          => 'lib/tool/file.class.php',
            'image'         => 'lib/tool/image.class.php',
            'pincode'       => 'lib/tool/pincode.class.php',
            'check'         => 'lib/tool/check.class.php',
            'player'        => 'lib/tool/player.class.php',
            'language'      => 'lib/tool/language.class.php',
            'upload'        => 'lib/tool/upload.class.php',
            'excel'         => 'lib/tool/excel.class.php',
            'curl'          => 'lib/tool/curl.class.php',
            'client'        => 'lib/tool/client.class.php',
            'zip'           => 'lib/tool/zip.class.php',
            'httpresponse'  => 'lib/tool/httpresponse.class.php',
            'validation'    => 'lib/tool/validation.class.php',
            'cache_memcache' => 'lib/tool/cache/cache_memcache.class.php',
            'cache_redis' => 'lib/tool/cache/cache_redis.class.php',
            'cache_xcache'  => 'lib/tool/cache/cache_xcache.class.php',
            'pinyin'        => 'lib/tool/pinyin.class.php',
            'text'          => 'lib/tool/text.class.php',
            'threadid'          => 'lib/tool/threadid.class.php',
            'thumb'          => 'lib/tool/thumb.class.php',
            'ftp'           => 'lib/tool/ftp.class.php',
            'mkhtml'        => 'lib/tool/mkhtml.class.php',
            'queue'         => 'lib/tool/queue.class.php',
            'tree'          => 'lib/tool/tree.class.php',
            'form'          => 'lib/tool/form.class.php',
            'cart'          => 'lib/tool/cart.class.php',
            'xml'           => 'lib/tool/xml.class.php',
            'csv'           => 'lib/tool/csv.class.php',
            'calendar'      => 'lib/tool/calendar.class.php',
            'http'          => 'lib/tool/http.class.php',
            'wsdl'          => 'lib/tool/wsdl.class.php',
            'encrypt'       => 'lib/tool/encrypt.class.php',
            'geohash'       => 'lib/tool/geohash.class.php',
            'cache_apc'     => 'lib/tool/cache/cache_apc.class.php',
            'cache_eaccelerator' => 'lib/tool/cache/cache_eaccelerator.class.php',
            'db_sqlite'     => 'lib/core/db/db_sqlite.class.php',
            'db_redis'      => 'lib/core/db/db_redis.class.php',
            'db_oracle'     => 'lib/core/db/db_oracle.class.php',
            'db_postgres'   => 'lib/core/db/db_postgres.class.php',
            'db_mssql'      => 'lib/core/db/db_mssql.class.php',
            'db_mongo'      => 'lib/core/db/db_mongo.class.php',

            //模板
            'TagLibCx' => 'modules/think/taglib/TagLibCx.class.php',
            'TagLibHtml' => 'modules/think/taglib/TagLibHtml.class.php',
            'TagLib' => 'modules/think/TagLib.class.php',

            //firephp
             'FB' => 'modules/firephp/FirePHPCore/fb.php',
             'FirePHP' => 'modules/firephp/FirePHPCore/FirePHP.class.php',
             'Phpexcel' => 'modules/phpexcel/PHPExcel.php',
             'PHPExcel_IOFactory' => 'modules/phpexcel/PHPExcel/IOFactory.php',
             'REST' => 'modules/ccpsms/ccpsms/CCPRestSmsSDK.class.php',
        );
    
    /**
     * 项目文件的自动加载

     * PDK 系统自动加载核心类库文件(core目录内的文件)及运行所需的controller文件、model文件、widget文件等

     * 注:并非程序初始化时将所有的controller,model等文件都统统加载完,再执行其它.理解本函数前一定要先理解AutoLoad的作用.
     * 当程序运行时发现所需的文件没有找到时,AutoLoad才会被激发,按照index()的程序设计来完成对该文件的加载
     * 
     * @access public 
     * @param string $className 所需要加载的类的名称,注:不含后缀名
     * @return void 
     */
     public static function index($className){
         // PDK 核心类文件的加载分析
        if (isset(self :: $coreClassArray[$className])){
             // 当$className在核心类引导数组中存在时, 加载核心类文件
            if(!is_file(self :: $coreClassArray[$className])){
                pdk ::loadFile(INCLUDE_PATH . self :: $coreClassArray[$className]);
            }else{
                pdk ::loadFile(self :: $coreClassArray[$className]);
            }
        }elseif (substr($className, -10) == 'Controller'){
             // controller文件自动载分析
            if (is_file(CONTROLLER_DIR . $className . '.class.php')){
                // 当文件在controller根目录下存在时,直接加载.
                pdk ::loadFile(CONTROLLER_DIR . $className . '.class.php');
            }else{
                 // 从controller的名称里获取子目录名称,注:controller文件的命名中下划线'_'相当于目录的'/'.
                    $pos = strpos($className, '_');
                 if ($pos !== false) {
                     // 当$controller中含有'_'字符时
                     $childDirName = strtolower(substr($className, 0, $pos));
                     $controllerFile = CONTROLLER_DIR . $childDirName . '/' . $className . '.class.php';
                     if (is_file($controllerFile)){
                     // 当子目录中所要加载的文件存在时
                        pdk ::loadFile($controllerFile);
                     }else{
                        // 当文件在子目录里没有找到时
                        Controller :: halt('The File:' . $className . '.class.php is not exists!');
                     }
                 }else{
                    // 当controller名称中不含有'_'字符串时
                    Controller :: halt('The File:' . $className . '.class.php is not exists!');
                 }
             }
         }else if (substr($className, -5) == 'Model'){
                 // modlel文件自动加载分析
                 if (is_file(MODEL_DIR . $className . '.class.php')){
                    // 当所要加载的model文件存在时
                    pdk ::loadFile(MODEL_DIR . $className . '.class.php');
                 }elseif (is_file(INC_MODELS_PATH . $className . '.class.php')){
                    // 当所要加载的model文件存在时
                    pdk ::loadFile(INC_MODELS_PATH . $className . '.class.php');
                }else{
                    // 当所要加载的文件不存在时,显示错误提示信息
//                     Controller :: halt('The Model file: ' . $className . ' is not exists!');
                 }
        }else if(substr($className, -6) == 'Widget'){
                 // 加载所要运行的widget文件
                 if (is_file(WIDGET_DIR . $className . '.class.php')){
                 // 当所要加载的widget文件存在时
                 pdk ::loadFile(WIDGET_DIR . $className . '.class.php');
             }else{
                 Controller :: halt('The Widget file: ' . $className . ' is not exists!');
             }
         }else if(strstr($className,'PHPExcel_')){

            $pClassFilePath = MODULE_DIR."phpexcel/" . str_replace('_',DIRECTORY_SEPARATOR,$className) .'.php';
            if(is_file($pClassFilePath)){
                pdk ::loadFile($pClassFilePath);
            }else{
                Controller :: halt('The module file: ' . $pClassFilePath . ' is not exists!');
            }

        }
        else if(strstr($className,'Qiniu\\')){
             $pClassFilePath = MODULE_DIR."qiniu/src/" . str_replace('\\',DIRECTORY_SEPARATOR,$className) .'.php';
            if(is_file($pClassFilePath)){
                pdk ::loadFile($pClassFilePath);
            }else{
                Controller :: halt('The module file: ' . $pClassFilePath . ' is not exists!');
            }

        }else{
             // 分析扩展目录文件
            if (is_file(EXTENSION_DIR . $className . '.class.php')){
                // 当扩展目录内文件存在时,则加载文件
                pdk ::loadFile(EXTENSION_DIR . $className . '.class.php');
            }else{
                 // 支持自定义auto load设置,适用于第三方扩展程序(模块)文件的自动加载
                 $autoloadConfigFile = CONFIG_DIR . 'autoload.ini.php';
                 if(!is_file($autoloadConfigFile)){
                     // 当所要加载的文件不存在时,提示错误信息
                    Controller :: halt('The File:' . $className . '.class.php is not exists!');
                 }
                 // 分析自定义自动加载
                 $autoloadArray = Controller :: getConfig('autoload');
                 $autoStatus = false;
                 foreach ((array)$autoloadArray as $rules){
                     // 将设置的规则中的*替换为所要加载的文件类名
                    $autoloadFile = str_replace('*', $className, $rules);
                     // 当自定义自动加载的文件存在时
                    if (is_file($autoloadFile)){
                         pdk ::loadFile($autoloadFile);
                         $autoStatus = true;
                         break;
                    }
                 }
                 // 当执行完自定义自动加载规则后,还没有找到所要加载的文件时,提示错误信息
                if ($autoStatus == false){
                     Controller :: halt('The file of class ' . $className . ' is not exists!');
                }
             }
         }
     }
}
